package fm.dongman.modellib.source;

import org.jetbrains.annotations.NotNull;

import fm.dongman.animefm.contractlib.contract.FRankContract;
import fm.dongman.animefm.contractlib.model.FRankDataModel;
import fm.dongman.modellib.local.FRankLocalDataSource;
import fm.dongman.modellib.remote.FRankRemoteDataSource;

/**
 * 排名
 * Created by KUIGE on 2017/10/14.
 */

public class FRankRepository implements FRankContract.IFRankDataSource {
    private static FRankRepository SINGLETON; // 实例
    private FRankRemoteDataSource mFRank;
    private FRankLocalDataSource mLocalDataSource;

    private FRankRepository(FRankRemoteDataSource mFRank, FRankLocalDataSource instance) {
        this.mLocalDataSource = instance;
        this.mFRank = mFRank;
    }

    /**
     * 返回单例
     */
    public static FRankRepository getInstance(FRankRemoteDataSource mFrank, FRankLocalDataSource instance) {
        if (SINGLETON == null) {
            SINGLETON = new FRankRepository(mFrank,instance);
        }
        return SINGLETON;
    }

    @Override
    public void getData(@NotNull final LoadSourceCallback<? super FRankDataModel> callback) {
        mFRank.getData(new LoadSourceCallback<FRankDataModel>() {
            @Override
            public void onDataLoaded(FRankDataModel dataModel) {
                callback.onDataLoaded(dataModel);
            }

            @Override
            public void onDataLoadFiled() {
                mLocalDataSource.getData(callback);
            }
        });
    }

    @Override
    public void saveData(FRankDataModel dataModel) {
        mLocalDataSource.saveData(dataModel);
    }

    @Override
    public void refreshData() {
    }

    @Override
    public void offLine() {
        mFRank.offLine();
    }

    @NotNull
    @Override
    public String getMoreRank() {
        return mFRank.getMoreRank();
    }
}
